/**********************************************************************************************************************
| Model Name    : GpioAbst
| File Name     : GpioAbst.c
|
| Description   : IO abstract model Initialize and provided interface.
|                 Only ues in MC9s12g     
|                 
|                               
|----------------------------------------------------------------------------------------------------------------------
|                                   C O P Y R I G H T
|----------------------------------------------------------------------------------------------------------------------
|   Copyright (c) by Zhejiang WanChao Electric .     All rights reserved.
|
|   This software is copyright protected and proprietary to Zhejiang WanChao Electric.
|
|   Zhejiang WanChao Electric grants to you only those rights as set out in the license conditions.  
|   
|   All other rights remain with Zhejiang WanChao Electric.
|
|----------------------------------------------------------------------------------------------------------------------
|                               A U T H O R   I D E N T I T Y
|----------------------------------------------------------------------------------------------------------------------
| Initials     Name                      Company
| --------     ---------------------     ------------------------------------------------------------------------------
| HAM           He Ai Min             Zhejiang WanChao Electric.
|----------------------------------------------------------------------------------------------------------------------
|               R E V I S I O N   H I S T O R Y
|----------------------------------------------------------------------------------------------------------------------
| Date       Version   Author  Description
|----------  --------  ------  ----------------------------------------------------------------------------------------
|2024-10-01  S1.0      HAM     - Creation
|*************************************************************************************************************************/


/*************************************************************************************************************************
Include File Segment
**************************************************************************************************************************/
#include "GpioAbst.h"

/*************************************************************************************************************************
Const Define Segment
**************************************************************************************************************************/


/*************************************************************************************************************************
Function Declare Segment
**************************************************************************************************************************/


/*************************************************************************************************************************
 Global Variable Define Segment
**************************************************************************************************************************/
GpioAbsCtrlSta_ST GpioAbsCtrlSta;
/*************************************************************************************************************************
| NAME:
| PROTOTYPE:
| CALLED BY:
| PRECONDITIONS:
| INPUT PARAMETERS:
| RETURN VALUE:
|
|
| DESCRIPTION:
|
|*************************************************************************************************************************/
void IF_GpioAbst_Init(void)
{
	FL_GPIO_InitTypeDef GPIO_InitStructure = {0};
#if 0		
	//GPIOA输入//KL15
	GPIO_InitStructure.pin = FL_GPIO_PIN_15;
	GPIO_InitStructure.mode = FL_GPIO_MODE_INPUT;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
    GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
	FL_GPIO_Init(GPIOH, &GPIO_InitStructure);
	
	//HAL1 PC9
	GPIO_InitStructure.pin = FL_GPIO_PIN_9;
	GPIO_InitStructure.mode = FL_GPIO_MODE_INPUT;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
	GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
	FL_GPIO_Init(GPIOC, &GPIO_InitStructure);

	//HAL2 HAL3 HAL4 PSW ; PE0,PE1,PE2,PE3
	GPIO_InitStructure.pin = FL_GPIO_PIN_0 | FL_GPIO_PIN_1 | FL_GPIO_PIN_2 | FL_GPIO_PIN_3;
	GPIO_InitStructure.mode = FL_GPIO_MODE_INPUT;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
	GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
	FL_GPIO_Init(GPIOE, &GPIO_InitStructure);
    
	
	//GPIOB输出//HAL_EN
	GPIO_InitStructure.pin = FL_GPIO_PIN_1 | FL_GPIO_PIN_2 | FL_GPIO_PIN_11;
	GPIO_InitStructure.mode = FL_GPIO_MODE_OUTPUT;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
	GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
    GPIO_InitStructure.remapPin   = FL_GPIO_PINREMAP_FUNCTON0;
    GPIO_InitStructure.driveStrength = FL_GPIO_DRIVESTRENGTH_X3;
	FL_GPIO_Init(GPIOG, &GPIO_InitStructure);

	//UBD12V: PA0,ADC_IN5//
	GPIO_InitStructure.pin = FL_GPIO_PIN_0;
	GPIO_InitStructure.mode = FL_GPIO_MODE_ANALOG;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
	GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
    GPIO_InitStructure.remapPin   = FL_GPIO_PINREMAP_FUNCTON0;
	FL_GPIO_Init(GPIOA, &GPIO_InitStructure);

	//VDD 5V :PC10
	GPIO_InitStructure.pin = FL_GPIO_PIN_8 | FL_GPIO_PIN_10; 
	GPIO_InitStructure.mode = FL_GPIO_MODE_ANALOG;
	GPIO_InitStructure.pull = FL_GPIO_BOTH_DISABLE;
	GPIO_InitStructure.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
    GPIO_InitStructure.remapPin   = FL_GPIO_PINREMAP_FUNCTON0;
	FL_GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	FL_GPIO_ResetOutputPin(GPIOG, FL_GPIO_PIN_2);//CAN_EN
	FL_GPIO_SetOutputPin(GPIOG, FL_GPIO_PIN_1);//PWRAD_EN
	FL_GPIO_ResetOutputPin(GPIOG, FL_GPIO_PIN_11);//HAL_EN

#else
	uint8 i;
    /* 使能时钟总线 */
    FL_CMU_EnableGroup1BusClock(FL_CMU_GROUP1_BUSCLK_PAD);
	for (i = 0; i < (sizeof(GpioCfgTable) / sizeof(GpioCfgStruct)); i++)
	{
		/* Pin脚模式设置 */
		IF_GPIO_SetPinMode(GpioCfgTable[i].Port, GpioCfgTable[i].Pin, GpioCfgTable[i].Dir);
		/* Pin脚上拉设置 */
		IF_GPIO_SetPinPull(GpioCfgTable[i].Port, GpioCfgTable[i].Pin, GpioCfgTable[i].PullUp);
		/* Pin脚输出初值 */
		if (GpioCfgTable[i].Dir == FL_GPIO_MODE_OUTPUT)
		IF_GPIO_WritePinData(GpioCfgTable[i].Port, GpioCfgTable[i].Pin, GpioCfgTable[i].InitLv);
	}
#endif	

}

/*************************************************************************************************************************
| 函 数 名: IF_GpioAbs_HallPwrCtrl      
| 输入参数: blState: ON:打开 OFF:关闭
| 返回参数: void
| 功能描述：线性霍尔电源控制
|*************************************************************************************************************************/
void IF_GpioAbs_HallPwrCtrl(uint8 blState)
{
    uint8 pinState;
	// 检查 blState 的合法性
    if (blState != ON && blState != OFF) {
        // 处理非法值，可以抛出错误或记录日志
        return;
    }

    // 根据 blState 设置霍尔电源开关
    pinState = (blState == ON) ? OFF : ON;

	IF_GPIO_WritePinData(PWR_EN_PORT, PWR_EN_PIN, pinState);

	// 更新 GpioAbsCtrlSta.PhoelecSwPwrCtrl 状态
	GpioAbsCtrlSta.PhoelecSwPwrCtrl = blState;

}

/*************************************************************************************************************************
| 函 数 名: IF_GpioAbs_UbdPwrAdcDetCtrl      
| 输入参数: blState
| 返回参数: null
| 功能描述：电源ADC检测控制
|*************************************************************************************************************************/
void IF_GpioAbs_UbdPwrAdcDetCtrl(uint8 blState)
{
    if (blState == ON)
	{
		IF_GPIO_WritePinData(PWR_AD_EN_PORT, PWR_AD_EN_PIN, ON);
		GpioAbsCtrlSta.UbdPwrAdcDetCtrl = ON;
	}
	else
	{
		IF_GPIO_WritePinData(PWR_AD_EN_PORT, PWR_AD_EN_PIN, OFF);
		GpioAbsCtrlSta.UbdPwrAdcDetCtrl = OFF;
	}	
}

/*************************************************************************************************************************
| 函 数 名: IF_GpioAbs_CanPwrCtrl      
| 输入参数: blState
| 返回参数: null
| 功能描述：CAN收发器打开关闭
|*************************************************************************************************************************/
void IF_GpioAbs_CanPwrCtrl(uint8 blState)
{
	if (blState == ON)
	{
		IF_GPIO_WritePinData(CAN_EN_PORT, CAN_EN_PIN, OFF);
		GpioAbsCtrlSta.CanPwrCtrl = ON;
	}
	else
	{
		IF_GPIO_WritePinData(CAN_EN_PORT, CAN_EN_PIN, ON);
		GpioAbsCtrlSta.CanPwrCtrl = OFF;
	}	
}

/*************************************************************************************************************************
| 函 数 名: IF_GpioAbs_GetIgState      
| 输入参数: null
| 返回参数: 低电平有效。即为低电平ON，高电平OFF
| 功能描述：获取KL15输入状态
|*************************************************************************************************************************/
uint8_t IF_GpioAbs_GetIgState(void)
{
	uint8_t ret = IF_GPIO_ReadPinData(IG_PORT, IG_PORT_PIN);
	return (ret == 0 ? ON : OFF);
}
